package com.tianyuan.dplrf.ui.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TableRow;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.WrapRecyclerView;
import com.tianyuan.dplrf.R;
import com.tianyuan.dplrf.ui.activity.Minenc_jtActivity;
import com.tianyuan.dplrf.ui.activity.PayLandActivity;
import com.tianyuan.dplrf.ui.adapter.Ncjt_f4_feil_Adapter;
import com.tianyuan.dplrf.ui.adapter.Ncjt_f4_tabs_adapter;
import com.tianyuan.dplrf.ui.bean.Ncjt_feilBean;

import java.util.ArrayList;
import java.util.List;

public class FragmentN4 extends FragmentLandBase {

    private WrapRecyclerView recycle_fls;
    private TableRow tabrow;
    private WrapRecyclerView recycle_tabs;
    private TableRow tabrow2;

    private Ncjt_f4_tabs_adapter tab_adapter;
    private List<String> tabs;
    private List<Ncjt_feilBean> myfl;
    private Ncjt_f4_feil_Adapter feil_adapter;
    private LinearLayout linearcontent;
    private FrameLayout frame_content;
    
    private int currentpage = 0;

    private View shouhview;
    private View dailView;
    private View feildetView;
    private AppCompatTextView edit_q1;
    private AppCompatButton btn_sure1;

    private AppCompatEditText edit_q2;
    private AppCompatButton btn_sure2;

    public static FragmentN4 newInstance() {
        return new FragmentN4();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_ncjt_glnc_layout;
    }

    @Override
    protected void initView() {

        recycle_fls = (WrapRecyclerView) findViewById(R.id.recycle_fls);
        tabrow = (TableRow) findViewById(R.id.tabrow);
        recycle_tabs = (WrapRecyclerView) findViewById(R.id.recycle_tabs);
        tabrow2 = (TableRow) findViewById(R.id.tabrow2);
        frame_content = (FrameLayout) findViewById(R.id.frame_content);

        linearcontent = (LinearLayout) findViewById(R.id.linearcontent);

        tab_adapter = new Ncjt_f4_tabs_adapter(getApplication());
        tabs = new ArrayList<>();
        tabs.add(getResources().getString(R.string.srt_feil));
        tabs.add(getResources().getString(R.string.srt_jiaos));
        tabs.add(getResources().getString(R.string.srt_shouh));
        tabs.add(getResources().getString(R.string.srt_daig));
        tab_adapter.setData(tabs);

        shouhview = LayoutInflater.from(getContext()).inflate(R.layout.fragment_ncjtshouh_layout,null);
        dailView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_ncjtdail_layout,null);
        feildetView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_ncjtfeildet_layout,null);
        tab_adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            private void initJsView(View jsview) {


                edit_q = jsview.findViewById(R.id.edit_q);
                btn_sure = jsview.findViewById(R.id.btn_sure);
                btn_sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //跳转 收货关闭
//                        Intent intent = new Intent(getActivity(), PayLandActivity.class);
//                        intent.putExtra("act_name",getActivity().getClass().getSimpleName());
//                        startActivity(intent);

                        toast("收货下工单！");
                        onBackClick();
                        onBackClick();
                    }
                });
            }

            private AppCompatButton btn_sure;
            private AppCompatEditText edit_q;

            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
//                tab_adapter.setCurrentSelect(position);
                Log.d("TAG", "onItemClick: " + position);
                if (position == 0) {
                    //肥料
                } else if (position == 1) {
                    //浇水
                    toast("下工单");
                    onBackClick();



                } else if (position == 2) {
                    //收获
                    if (shouhview.getParent() != null){
                        shouhview.setVisibility(View.VISIBLE);
                    }else {
                        frame_content.addView(shouhview);
                        initJsView(shouhview);
                    }
                    linearcontent.setVisibility(View.GONE);
                    currentpage = 1;
                } else if (position == 3) {
                    //代管
                    if (dailView.getParent() != null){
                        dailView.setVisibility(View.VISIBLE);
                    }else {
                        frame_content.addView(dailView);
                        initDailView(dailView);
                    }
                    linearcontent.setVisibility(View.GONE);
                    currentpage = 2;
                }
            }
        });
        recycle_tabs.setLayoutManager(new LinearLayoutManager(getApplication(),RecyclerView.VERTICAL,false));
        recycle_tabs.setAdapter(tab_adapter);

        feil_adapter = new Ncjt_f4_feil_Adapter(getApplication());
        myfl = new ArrayList<>();
        addData("10kg",R.drawable.img_fl1);
        addData("2kg",R.drawable.img_fl2);

        addData("5kg",R.drawable.img_fl1);
        addData("4kg",R.drawable.img_fl2);

        feil_adapter.setData(myfl);
        feil_adapter.setOnItemClickListener((RecyclerView recyclerView, View itemView, int position)->{
            Log.d("TAG", "onItemClick2: " + position);
            if (feildetView.getParent() != null){
                feildetView.setVisibility(View.VISIBLE);
                initFeilView(feildetView,position,true);
            }else {
                frame_content.addView(feildetView);
                initFeilView(feildetView,position,false);
            }
            linearcontent.setVisibility(View.GONE);
            currentpage = 3;
        });
        recycle_fls.setLayoutManager(new LinearLayoutManager(getApplication(),RecyclerView.VERTICAL,false));
        recycle_fls.setAdapter(feil_adapter);


    }


    private void addData(String name, int imgFl1) {
        Ncjt_feilBean bean = new Ncjt_feilBean();
        bean.setDesc(name);
        bean.setImg(imgFl1);
        myfl.add(bean);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onBackClick() {
        if (currentpage == 0) {
            ((Minenc_jtActivity) getActivity()).onPopfinish();
        } else if (currentpage == 1) {
            currentpage = 0;
            shouhview.setVisibility(View.GONE);
            linearcontent.setVisibility(View.VISIBLE);

        } else if (currentpage == 2) {
            currentpage = 0;
            dailView.setVisibility(View.GONE);
            linearcontent.setVisibility(View.VISIBLE);
        }else if (currentpage == 3) {
            currentpage = 0;
            feildetView.setVisibility(View.GONE);
            linearcontent.setVisibility(View.VISIBLE);
        }
    }

    private void initDailView(View jsview) {
        edit_q1 = jsview.findViewById(R.id.edit_q);
        btn_sure1 = jsview.findViewById(R.id.btn_sure);
        btn_sure1.setOnClickListener((View v)->{
            toast("确认代理");
            onBackClick();
            onBackClick();
        });
    }

    private void initFeilView(View jsview,int position,boolean isAttach) {
        if (!isAttach) {
            edit_q2 = jsview.findViewById(R.id.edit_q);
            btn_sure2 = jsview.findViewById(R.id.btn_sure);

            btn_sure2.setOnClickListener((View v) -> {
                toast("施肥。。");
                onBackClick();
                onBackClick();
            });
        }
        //根据position设置文本 或是其他图片
//        myfl.get(position);
    }
}
